<template>
  <div>
    <el-button slot="reference" @click="showCompleted">维修申请已处理</el-button>
    <el-button slot="reference" @click="showUncompleted">维修申请未处理</el-button>
    <el-table :data="tableData">
      <el-table-column prop="createdTime" label="日期" width="100" :formatter="formatDate">
      </el-table-column>
      <el-table-column prop="studentName" label="姓名" width="100">
      </el-table-column>
      <el-table-column prop="buildingName" label="楼栋" width="100">
      </el-table-column>
      <el-table-column prop="roomName" label="房间" width="100">
      </el-table-column>
      <el-table-column prop="reason" label="原因" width="100">
      </el-table-column>
      <el-table-column prop="keeper" label="维修人员" width="100">
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100">
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button
              v-if="isCompleted"
              size="small"
              type="success"
              disabled>
            已完成
          </el-button>
          <el-button
              v-else
              size="small"
              type="primary"
              @click="handleAllocation(scope.row)">
            分配
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageNo"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total=totalNum>
      </el-pagination>
    </div>
    <el-dialog title="分配维修人员" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item prop="createdTime" label="日期:" width="140">
          {{allocationForm.createdTime}}
        </el-form-item>
        <el-form-item prop="studentName" label="姓名:" width="120">
          {{allocationForm.studentName}}
        </el-form-item>
        <el-form-item prop="buildingName" label="楼栋:" width="120">
          {{allocationForm.buildingName}}
        </el-form-item>
        <el-form-item prop="roomName" label="房间:" width="120">
          {{allocationForm.roomName}}
        </el-form-item>
        <el-form-item prop="reason" label="原因:" width="120">
          {{allocationForm.reason}}
        </el-form-item>
        <el-form-item label="维修人员:" :label-width="100">
          <el-select v-model="allocationForm.keeper" placeholder="请选择维修人员">
            <el-option
                v-for="item in keepers"
                :key="item.name"
                :label="`${item.name} 待完成任务数:${item.tasks}`"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sendAllocation">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {selectRepair, getKeepers, updateRepair, selectDoneRepair} from '../../../api/dormitory_admin'
export default {
  data () {
    return {
      pageNo: 1,
      pageSize: 5,
      dialogFormVisible: false,
      DOT: {
        pageNo: 1,
        pageSize: 5,
        data: {
          ID: JSON.parse(localStorage.getItem('user')).id.toString(),
          state: '已完成'
        }
      },
      keepers: [],
      isCompleted: true,
      totalNum: 0,
      selectForm: {
        building: '',
        inRoom: ''
      },
      allocationForm: {
        id: null,
        createdTime: null,
        studentName: null,
        buildingName: null,
        roomName: null,
        reason: null,
        keeper: null
      },
      tableData: []
    }
  },
  methods: {
    formatDate (row) {
      const date = new Date(row.createdTime)
      const beijingTime = new Date(date.getTime())
      const year = beijingTime.getFullYear()
      const month = (beijingTime.getMonth() + 1).toString().padStart(2, '0')
      const day = beijingTime.getDate().toString().padStart(2, '0')
      return `${year}年${month}月${day}日`
    },
    handleCurrentChange (newPage) {
      this.DOT.pageNo = newPage
      this.pageNo = newPage
      if (this.isCompleted) {
        this.getCompleted()
      } else {
        this.getUncompleted()
      }
    },
    showCompleted () {
      this.isCompleted = true
      this.DOT.pageNo = 1
      this.pageNo = 1
      this.getCompleted()
    },
    getCompleted () {
      selectDoneRepair(this.DOT).then(res => {
        this.tableData = res.data
        this.totalNum = res.otherData.totalNum
      })
    },
    showUncompleted () {
      this.DOT.pageNo = 1
      this.pageNo = 1
      this.totalNum = 0
      this.isCompleted = false
      this.getUncompleted()
    },
    getUncompleted () {
      selectRepair(this.DOT).then(res => {
        this.tableData = res.data
        this.totalNum = res.otherData.totalNum
      })
    },
    handleAllocation (row) {
      console.log(this.keepers)
      this.allocationForm = row
      this.allocationForm.ID = row.ID
      this.dialogFormVisible = true
      console.log(this.allocationForm)
    },
    sendAllocation () {
      if (this.allocationForm.keeper === null || this.allocationForm.keeper === '') {
        this.$message({
          type: 'error',
          message: '请选择维修人员!'
        })
        return
      }
      this.dialogFormVisible = false
      let obj = {
        repairID: this.allocationForm.id.toString(),
        keeperID: this.allocationForm.keeper.toString()
      }
      updateRepair(obj).then(res => {
        if (res.status) {
          this.$message({
            message: '分配成功',
            type: 'success'
          })
          this.showUncompleted()
        } else {
          this.$message({
            message: '分配失败',
            type: 'error'
          })
        }
      })
    }
  },
  created () {
    getKeepers().then(res => {
      this.keepers = res.data
    })
    this.showCompleted()
  }
}
</script>
<style scoped>
</style>
